﻿#include "MemsAutoTestSoftware.h"

#include "SetScriptDialog.h"
#include "DeviceInfoManager.h"
//#include "ScriptSaveLoader.h"
#include "LogSaver.h"
#include "TcpCodec_Transmit.h"

#include "Device.h"

//ui
#include "Dialog_SetThermostat.h"
#include "Dialog_SetLoadMode.h"
#include "Dialog_SetRunningMode.h"
#include "MainWindow.h"

#include <qcolor.h>

#include <qdebug.h>

MemsAutoTestSoftware::MemsAutoTestSoftware(QWidget* parent) :QMainWindow(parent)
{
  ui.setupUi(this);
  ui.action_set_script->setDisabled(true);
  ui.menu_control_script->setDisabled(true);
  ui.action_file_saveScript->setDisabled(true);
  ui.action_file_loadScript->setDisabled(true);
  initAllUI();
  initSignalsAndSlots();
}

MemsAutoTestSoftware::~MemsAutoTestSoftware()
{
  disconnect();
}

void MemsAutoTestSoftware::showDevice(uint deviceId)
{
  this->deviceId=deviceId;
  show();
}

void MemsAutoTestSoftware::slot_updateTurnTableState()
{
  if(!isVisible())
    {
      return;
    }
  Device* device=deviceInfoManager->getDeviceInfoByDeviceId(deviceId);
  if(Q_NULLPTR==device)
    {
      return;
    }
  float position=device->getTurnTable_currentPosition();
  float rate=device->getTurnTable_currentRate();
  ui.label_turnTable_currentPosition->setText(QString::number(position, 'f', accuracy));
  ui.label_turnTable_currentRate->setText(QString::number(rate, 'f', accuracy));
  bool isPowerOn=device->getTurnTable_runningState_power(),
      isCircuitClose=device->getTurnTable_runningState_circuit(),
      isAmplifierBroken=device->getTurnTable_runningState_amplifier(),
      isRunning=device->getTurnTable_runningState_running();
  if (isRunning)
    {
      ui.frame_turnTableStateShower->paintNewState(position,rate,device->getTurnTable_targetRate());
    }
  ui.lineEdit_currentTemperature->setText(QString::number(device->getTurnTable_thermostat_currentTemperature(), 'f', accuracy));
  if(isPowerOn)//判断设备电源状态
    {//上电状态
      ui.label_turnTable_stateFlag_powerSupply->setStyleSheet("background:green");
      ui.action_control_turnTable_powerOn->setDisabled(true);
      if (isCircuitClose)//判断设备电路状态
        {//闭合状态
          ui.action_control_turnTable_powerOff->setDisabled(true);
        }
      else
        {//闲置状态
          ui.action_control_turnTable_powerOff->setDisabled(false);
        }
    }
  else//下电状态
    {
      ui.label_turnTable_stateFlag_powerSupply->setStyleSheet("background:red");
      ui.action_control_turnTable_powerOn->setDisabled(false);
      ui.action_control_turnTable_powerOff->setDisabled(true);
    }
  if(isCircuitClose)//判断电路状态
    {//闭合状态
      ui.label_turnTable_stateFlag_circuit->setStyleSheet("background:green");
      ui.action_control_turnTable_closeCircuit->setDisabled(true);
      if (isRunning)//判断转台运行状态
        {//运行
          ui.action_control_turnTable_openCircuit->setDisabled(true);
        }
      else
        {//停止
          ui.action_control_turnTable_openCircuit->setDisabled(false);
        }
    }
  else//闲置状态
    {
      ui.label_turnTable_stateFlag_circuit->setStyleSheet("background:red");
      ui.action_control_turnTable_openCircuit->setDisabled(true);
      if (isPowerOn)
        {
          ui.action_control_turnTable_closeCircuit->setDisabled(false);
        }
      else
        {
          ui.action_control_turnTable_closeCircuit->setDisabled(true);
        }
    }
  if(isAmplifierBroken)//判断功放机状态，true是有问题，false是正常
    {//有问题
      ui.label_turnTable_stateFlag_amplifier->setStyleSheet("background:red");
    }
  else
    {//没问题
      ui.label_turnTable_stateFlag_amplifier->setStyleSheet("background:green");
    }
  if(isRunning)//判断运行状态
    {//正在运行
      ui.label_turnTable_stateFlag_running->setStyleSheet("background:green");
      ui.action_control_turnTable_run->setDisabled(true);
      ui.action_control_turnTable_stop->setDisabled(false);
      ui.action_control_turnTable_emergencyStop->setDisabled(false);
    }
  else//停止运行
    {
      ui.label_turnTable_stateFlag_running->setStyleSheet("background:red");
      ui.action_control_turnTable_stop->setDisabled(true);
      ui.action_control_turnTable_emergencyStop->setDisabled(true);
      if (isCircuitClose)//判断电路状态
        {//闭合
          ui.action_control_turnTable_run->setDisabled(false);
        }
      else//闲置
        {
          ui.action_control_turnTable_run->setDisabled(true);
        }
    }
  bool runningMode=device->getTurnTable_runningMode();//返回true代表速率模式，false代表位置模式
  float targetPosition=device->getTurnTable_targetPosition(),
      targetRate=device->getTurnTable_targetRate(),
      targetAcceleration=device->getTurnTable_targetAcceleration();
  if(runningMode)
    {
      ui.lineEdit_turnTable_runMode->setText(QStringLiteral("速率模式"));
      ui.lineEdit_turnTable_targetPosition->setText("---");
      ui.lineEdit_turnTable_targetRate->setText(QString::number(targetRate, 'f', accuracy));
      ui.lineEdit_turnTable_targetAcceleration->setText(QString::number(targetAcceleration, 'f', accuracy));
    }
  else
    {
      ui.lineEdit_turnTable_runMode->setText(QStringLiteral("位置模式"));
      ui.lineEdit_turnTable_targetPosition->setText(QString::number(targetPosition, 'f', accuracy));
      ui.lineEdit_turnTable_targetRate->setText(QString::number(targetRate, 'f', accuracy));
      ui.lineEdit_turnTable_targetAcceleration->setText(QString::number(targetAcceleration, 'f', accuracy));
    }
  ui.lineEdit_targetTemperature->setText(QString::number(device->getTurnTable_thermostat_targetTemperature(),'f',accuracy));
  ui.lineEdit_temperatureRate->setText(QString::number(device->getTurnTable_thermostat_targetTemperatureRate(),'f',accuracy));
}

void MemsAutoTestSoftware::slot_file_loadScript()//打开加载脚本对话框
{
  logSaver->saveLog(QStringLiteral("打开加载脚本对话框"), LogSaver::Information);
  QFileDialog dialog(this);
  dialog.setAcceptMode(QFileDialog::AcceptOpen);
  dialog.setNameFilter(".script(*.script)");
  QString filepath;
  if (QDialog::Accepted == dialog.exec())
    {
      filepath = dialog.selectedFiles().first();
      if (!filepath.isEmpty())
        {
          //          scriptSaveLoader->loadScriptFile(filepath);
        }
    }
  return;
}
void MemsAutoTestSoftware::slot_file_saveScript()//打开保存脚本对话框
{
  logSaver->saveLog(QStringLiteral("打开保存脚本对话框"), LogSaver::Information);
  QFileDialog dialog(this);
  dialog.setAcceptMode(QFileDialog::AcceptSave);
  dialog.setNameFilter(".script(*.script)");
  QString filepath;
  if (dialog.exec()) {
      filepath = dialog.selectedFiles().first();
    }
  if (!filepath.isEmpty()) {
      //      if (!(scriptSaveLoader->saveAsScriptFile(filepath)))
      //        {
      //          showMessage(QStringLiteral("保存脚本时格式转换出错"));
      //        }
    }
  return;
}
void MemsAutoTestSoftware::slot_file_about()//关于转台
{
  QMessageBox::information(this, QStringLiteral("关于"), QStringLiteral("本软件只保证每个操作本身的正确性，具体是否应该执行某个操作，请三思而后行。"));
}

void MemsAutoTestSoftware::slot_control_turnTable_run()
{
  TurnTableCodec::TurnTableRunningMode runningMode = dialog_SetRunningMode->getRunningMode()?TurnTableCodec::Mode_RateMode:TurnTableCodec::Mode_PositionMode;
  turnTableCodec->sendInstruction(deviceId,TurnTableCodec::TurnTable_Run, runningMode, 0, 0);
  showMessage(QStringLiteral("发送指令：运行转台"));
}
void MemsAutoTestSoftware::slot_control_thermostat_run()
{
  float temperature=dialog_setThermostat->getTemperature();
  turnTableCodec->sendInstruction(deviceId,TurnTableCodec::Thermostat_TargetTemperature,temperature,0,0);
  showMessage(QStringLiteral("发送指令：设置温箱温度，%1℃").arg(QString::number(temperature,'f',accuracy)));
}

void MemsAutoTestSoftware::slot_set_turnTable_loadMode()
{
  if(QDialog::Accepted==dialog_SetLoadMode->exec())
    {
      turnTableCodec->sendInstruction(deviceId,TurnTableCodec::TurnTable_SetLoad,dialog_SetLoadMode->getLoad(),0,0);
      showMessage(QStringLiteral("发送指令：设置为负载模式"));
    }
}

void MemsAutoTestSoftware::slot_set_turnTable_runningMode()
{
  if(QDialog::Accepted==dialog_SetRunningMode->exec())
    {
      float positon=dialog_SetRunningMode->getPosition(),
          rate=dialog_SetRunningMode->getRate(),
          acceleration=dialog_SetRunningMode->getAcceleration();
      if(dialog_SetRunningMode->getRunningMode())//结果为true则为速率模式，否则为位置模式
        {//速率模式
          turnTableCodec->sendInstruction(deviceId,TurnTableCodec::TurnTable_SetRateMode,rate,acceleration,0);
          showMessage(QStringLiteral("发送指令：设置为速率模式, %1°/s , %2°/s ²").arg(QString::number(rate,'f',4)).arg(QString::number(acceleration,'f',4)));
        }
      else//位置模式
        {
          turnTableCodec->sendInstruction(deviceId,TurnTableCodec::TurnTable_SetPositionMode,positon,rate,acceleration);
          showMessage(QStringLiteral("发送指令：设置为位置模式, %1°, %2°/s , %3°/s ²").arg(QString::number(positon,'f',4)).arg(QString::number(rate,'f',4)).arg(QString::number(acceleration,'f',4)));
        }

    }
}

void MemsAutoTestSoftware::slot_set_thermostat_targetTemperature()
{
  if(QDialog::Accepted==dialog_setThermostat->exec())
    {
      float rate=dialog_setThermostat->getRate();
      turnTableCodec->sendInstruction(deviceId,TurnTableCodec::Thermostat_TemperatureRate,rate,0,0);
      showMessage(QStringLiteral("发送指令：设置温箱速率，%1℃/s").arg(QString::number(rate,'f',4)));
    }
}

void MemsAutoTestSoftware::slot_set_script()
{
  setScriptDialog->exec();
}

//void MemsAutoTestSoftware::slot_updateTurnTableConnectionState(TurnTableCodec::ConnectionState connectionState)
//{
//  switch (connectionState)
//    {
//    case TurnTableCodec::State_Disconnected:
//      //转台温箱连接状态指示灯
//      ui.label_turnTable_stateFlag_connection->setStyleSheet("background:red");
//      ui.label_thermostat_stateFlag_connection->setStyleSheet("background:lightgray");

//      //转台运行状态指示灯
//      ui.label_turnTable_stateFlag_powerSupply->setStyleSheet("background:lightgray");
//      ui.label_turnTable_stateFlag_circuit->setStyleSheet("background:lightgray");
//      ui.label_turnTable_stateFlag_running->setStyleSheet("background:lightgray");
//      ui.label_turnTable_stateFlag_amplifier->setStyleSheet("background:lightgray");

//      //对应每个与转台连接相关的按钮
//      ui.action_control_turnTable_connect->setDisabled(false);
//      ui.action_control_turnTable_disconnect->setDisabled(true);
//      ui.action_control_turnTable_powerOn->setDisabled(true);
//      ui.action_control_turnTable_powerOff->setDisabled(true);
//      ui.action_control_turnTable_closeCircuit->setDisabled(true);
//      ui.action_control_turnTable_openCircuit->setDisabled(true);
//      ui.action_control_turnTable_run->setDisabled(true);
//      ui.action_control_turnTable_stop->setDisabled(true);
//      ui.action_control_turnTable_emergencyStop->setDisabled(true);
//      ui.action_control_thermostat_connect->setDisabled(true);
//      ui.action_control_thermostat_disconnect->setDisabled(true);
//      ui.action_control_thermostat_run->setDisabled(true);

//      //数值显示
//      ui.label_turnTable_currentPosition->setText("---");
//      ui.label_turnTable_currentRate->setText("---");
//      ui.lineEdit_turnTable_runMode->setText("---");
//      ui.lineEdit_turnTable_targetPosition->setText("---");
//      ui.lineEdit_turnTable_targetRate->setText("---");
//      ui.lineEdit_turnTable_targetAcceleration->setText("---");
//      ui.lineEdit_currentTemperature->setText("---");
//      ui.lineEdit_targetTemperature->setText("---");
//      ui.lineEdit_temperatureRate->setText("---");
//      showMessage(QStringLiteral("已从转台断开"));
//      break;
//    case TurnTableCodec::State_Connected:
//      ui.label_turnTable_stateFlag_connection->setStyleSheet("background:green");
//      ui.label_thermostat_stateFlag_connection->setStyleSheet("background:red");
//      ui.action_control_turnTable_connect->setDisabled(true);
//      ui.action_control_turnTable_disconnect->setDisabled(false);
//      ui.action_control_thermostat_connect->setDisabled(false);
//      showMessage(QStringLiteral("已连接到转台"));
//      break;
//    case TurnTableCodec::State_Connecting:
//      ui.label_turnTable_stateFlag_connection->setStyleSheet("background:yellow");
//      ui.action_control_turnTable_connect->setDisabled(true);
//      ui.action_control_turnTable_disconnect->setDisabled(true);
//      showMessage(QStringLiteral("正在连接转台"));
//      break;
//    default:
//      break;
//    }
//}

void MemsAutoTestSoftware::slot_turnTableErrorEcho(TurnTableCodec::ErrorFlag errorFlag)
{
  switch (errorFlag)
    {
    case TurnTableCodec::Error_ProcessState:
      showMessage(QStringLiteral("转台错误：过程状态错误"));
      break;
    case TurnTableCodec::Error_AxleState:
      showMessage(QStringLiteral("转台错误：轴状态错误"));
      break;
    case TurnTableCodec::Error_AxleNum:
      showMessage(QStringLiteral("转台错误：轴号错误"));
      break;
    case TurnTableCodec::Error_CheckSummation:
      showMessage(QStringLiteral("转台错误：检查和错误"));
      break;
    case TurnTableCodec::Error_TurnTableAddress:
      showMessage(QStringLiteral("转台错误：转台设备地址错误"));
      break;
    case TurnTableCodec::Error_Load:
      showMessage(QStringLiteral("转台错误：负载错误"));
      break;
    case TurnTableCodec::Error_PositionModeOverLimited:
      showMessage(QStringLiteral("转台错误：位置模式参数超限"));
      break;
    case TurnTableCodec::Error_RateModeOverLimited:
      showMessage(QStringLiteral("转台错误：速率模式参数超限"));
      break;
    case TurnTableCodec::Error_OtherUnfitCondition:
      showMessage(QStringLiteral("转台错误：其他不符合限制条件的错误"));
      break;
    case TurnTableCodec::Error_ThermostatTargetTemperatureFailed:
      showMessage(QStringLiteral("转台错误：温箱目标温度设置失败"));
      break;
    case TurnTableCodec::Error_ThermostatTargetTemperatureOverLimited:
      showMessage(QStringLiteral("转台错误：温箱目标温度超限"));
      break;
    case TurnTableCodec::Error_ThermostatFeedbackError:
      showMessage(QStringLiteral("转台错误：温箱应答帧错误"));
      break;
    case TurnTableCodec::Error_Connection:
      showMessage(QStringLiteral("转台错误：建立通讯错误"));
      break;
    default:
      break;
    }
}

void MemsAutoTestSoftware::slot_showResult(uchar result)
{
  showMessage(tcpCodec_Transmit->getResultMessage(result));
}

//脚本管理器反馈
void MemsAutoTestSoftware::slot_scriptIsStarted()
{
  ui.action_file_saveScript->setDisabled(true);
  ui.action_file_loadScript->setDisabled(true);
  ui.action_control_script_startScript->setDisabled(true);
  ui.action_control_script_stopScript->setDisabled(false);
  ui.menu_control_turnTable->setDisabled(true);
  ui.menu_control_thermostat->setDisabled(true);
  //  ui.action_set_turnTableAndThermostat->setDisabled(true);
  ui.action_set_script->setDisabled(true);
  showMessage(QStringLiteral("脚本已开始运行"));
}
void MemsAutoTestSoftware::slot_scriptIsStoped()
{
  ui.action_file_saveScript->setDisabled(false);
  ui.action_file_loadScript->setDisabled(false);
  ui.action_control_script_startScript->setDisabled(false);
  ui.action_control_script_stopScript->setDisabled(true);
  ui.menu_control_turnTable->setDisabled(false);
  ui.menu_control_thermostat->setDisabled(false);
  //  ui.action_set_turnTableAndThermostat->setDisabled(false);
  ui.action_set_script->setDisabled(false);
  showMessage(QStringLiteral("脚本已停止运行"));
}

void MemsAutoTestSoftware::slot_loadScriptFileFailed(QString filePath)
{
  showMessage(QStringLiteral("加载脚本文件%1失败").arg(filePath));
}
void MemsAutoTestSoftware::slot_saveScriptFileFailed(QString filePath)
{
  showMessage(QStringLiteral("保存脚本文件%1失败").arg(filePath));
}

//初始化信号槽，暂时由对象管理器调用
void MemsAutoTestSoftware::initSignalsAndSlots()
{
  //各个菜单按钮的信号槽
  //  connect(ui.action_file_saveScript, &QAction::triggered, this, &MemsAutoTestSoftware::slot_file_saveScript);
  //  connect(ui.action_file_loadScript, &QAction::triggered, this, &MemsAutoTestSoftware::slot_file_loadScript);
  connect(ui.action_file_about, &QAction::triggered, this, &MemsAutoTestSoftware::slot_file_about);
  connect(ui.action_file_quit, &QAction::triggered, [this]() {close(); });
  connect(ui.action_control_turnTable_powerOn, &QAction::triggered, [this]()
  {
      turnTableCodec->sendInstruction(deviceId,TurnTableCodec::TurnTable_PowerOn, 0, 0, 0);
      showMessage(QStringLiteral("发送指令：转台上电"));
    });
  connect(ui.action_control_turnTable_powerOff, &QAction::triggered, [this]()
  {
      turnTableCodec->sendInstruction(deviceId,TurnTableCodec::TurnTable_PowerOff, 0, 0, 0);
      showMessage(QStringLiteral("发送指令：转台下电"));
    });
  connect(ui.action_control_turnTable_closeCircuit, &QAction::triggered, [this]()
  {
      turnTableCodec->sendInstruction(deviceId,TurnTableCodec::TurnTable_CloseCircuit, 0, 0, 0);
      showMessage(QStringLiteral("发送指令：转台闭合"));
    });
  connect(ui.action_control_turnTable_openCircuit, &QAction::triggered, [this]()
  {
      turnTableCodec->sendInstruction(deviceId,TurnTableCodec::TurnTable_OpenCircuit, 0, 0, 0);
      showMessage(QStringLiteral("发送指令：转台闲置"));
    });
  connect(ui.action_control_turnTable_run, &QAction::triggered, this, &MemsAutoTestSoftware::slot_control_turnTable_run);
  connect(ui.action_control_turnTable_stop, &QAction::triggered, [this]()
  {
      turnTableCodec->sendInstruction(deviceId,TurnTableCodec::TurnTable_Stop, 0, 0, 0);
      showMessage(QStringLiteral("发送指令：转台停止"));
    });
    connect(ui.action_control_turnTable_emergencyStop, &QAction::triggered, [this]()
    {
        turnTableCodec->sendInstruction(deviceId,TurnTableCodec::TurnTable_EmergencyStop, 0, 0, 0);
      showMessage(QStringLiteral("发送指令：转台急停"));
      });
//    connect(ui.action_control_thermostat_connect, &QAction::triggered, [this]() {turnTableCodec->sendInstruction(TurnTableCodec::Thermostat_Connect, 0, 0, 0); });
  //  connect(ui.action_control_thermostat_disconnect, &QAction::triggered, [this]() {turnTableCodec->sendInstruction(TurnTableCodec::Thermostat_Disconnect, 0, 0, 0); });
  connect(ui.action_control_thermostat_run, &QAction::triggered, this, &MemsAutoTestSoftware::slot_control_thermostat_run);
  connect(ui.action_set_turnTable_loadMode, &QAction::triggered,this,&MemsAutoTestSoftware::slot_set_turnTable_loadMode);
  connect(ui.action_set_turnTable_runningMode, &QAction::triggered,this,&MemsAutoTestSoftware::slot_set_turnTable_runningMode);
  connect(ui.action_set_thermostat_targetTemperature, &QAction::triggered,this,&MemsAutoTestSoftware::slot_set_thermostat_targetTemperature);
//  connect(ui.action_set_script, &QAction::triggered,this,&MemsAutoTestSoftware::slot_set_script);
}

void MemsAutoTestSoftware::showMessage(QString msg)
{
  QListWidget* listWidget = ui.listWidget_message;
  listWidget->addItem(QDateTime::currentDateTime().toString("yyyy" + QStringLiteral("年") + "MM" + QStringLiteral("月") + "dd" + QStringLiteral("日") + "  hh:mm:ss  ") + msg);
  listWidget->setCurrentRow(listWidget->count() - 1);
  if (listWidget->count() > 100)
    {//if the count of listWidget's item over 100
      listWidget->takeItem(0);//remove the first one
    }
}

void MemsAutoTestSoftware::initAllUI()
{
  height();

  //setFixedSize(this->width(),this->height());
  Qt::WindowFlags windowFlag = Qt::Dialog;//最大化最小化和关闭按钮
  windowFlag |= Qt::WindowMinimizeButtonHint;
  windowFlag |= Qt::WindowMaximizeButtonHint;
  windowFlag |= Qt::WindowCloseButtonHint;
  setWindowFlags(windowFlag);
  //ui.statusBar->setSizeGripEnabled(false);//禁用状态栏右下角调整大小的拖拽点

  //转台运行状态指示灯
  ui.label_turnTable_stateFlag_powerSupply->setStyleSheet("background:lightgray");
  ui.label_turnTable_stateFlag_circuit->setStyleSheet("background:lightgray");
  ui.label_turnTable_stateFlag_running->setStyleSheet("background:lightgray");
  ui.label_turnTable_stateFlag_amplifier->setStyleSheet("background:lightgray");

  //对应每个与转台连接相关的按钮
  ui.action_control_turnTable_powerOn->setDisabled(true);
  ui.action_control_turnTable_powerOff->setDisabled(true);
  ui.action_control_turnTable_closeCircuit->setDisabled(true);
  ui.action_control_turnTable_openCircuit->setDisabled(true);
  ui.action_control_turnTable_run->setDisabled(true);
  ui.action_control_turnTable_stop->setDisabled(true);
  ui.action_control_turnTable_emergencyStop->setDisabled(true);
  ui.action_control_thermostat_connect->setDisabled(true);
  ui.action_control_thermostat_disconnect->setDisabled(true);
  ui.action_control_thermostat_run->setDisabled(true);

  //数值显示
  ui.label_turnTable_currentPosition->setText("---");
  ui.label_turnTable_currentRate->setText("---");
  ui.lineEdit_turnTable_runMode->setText("---");
  ui.lineEdit_turnTable_targetPosition->setText("---");
  ui.lineEdit_turnTable_targetRate->setText("---");
  ui.lineEdit_turnTable_targetAcceleration->setText("---");
  ui.lineEdit_currentTemperature->setText("---");
  ui.lineEdit_targetTemperature->setText("---");
  ui.lineEdit_temperatureRate->setText("---");
}

void MemsAutoTestSoftware::closeEvent(QCloseEvent* event)
{
  if (QMessageBox::question(this, QStringLiteral("退出设备控制"), QStringLiteral("确定要退出当前设备控制吗？")) == QMessageBox::Yes)
    {
      //      event->accept();
      hide();
      mainWindow->show();
    }
  else
    {
      event->ignore();
    }
}

void MemsAutoTestSoftware::showEvent(QShowEvent *event)
{
  slot_updateTurnTableState();
  QMainWindow::showEvent(event);
}

uint MemsAutoTestSoftware::getDeviceId()const
{
  return deviceId;
}

MemsAutoTestSoftware* MemsAutoTestSoftware::setDeviceId(uint deviceId)
{
  this->deviceId=deviceId;
  dialog_setThermostat->setDeviceId(deviceId);
  dialog_SetRunningMode->setDeviceId(deviceId);
  return this;
}

DeviceInfoManager* MemsAutoTestSoftware::getDeviceInfoManager()const
{
  return deviceInfoManager;
}

MemsAutoTestSoftware* MemsAutoTestSoftware::setDeviceInfoManager(DeviceInfoManager* const deviceInfoManager)
{
  if(Q_NULLPTR!=this->deviceInfoManager)
    {
      disconnect(this->deviceInfoManager,&DeviceInfoManager::signal_newAllDeviceInfo,this,&MemsAutoTestSoftware::slot_updateTurnTableState);
    }
  this->deviceInfoManager=deviceInfoManager;
  printInjection(this,deviceInfoManager);
  if(Q_NULLPTR!=this->deviceInfoManager)
    {
      connect(this->deviceInfoManager,&DeviceInfoManager::signal_newAllDeviceInfo,this,&MemsAutoTestSoftware::slot_updateTurnTableState);
    }
  return this;
}

TurnTableCodec* MemsAutoTestSoftware::getTurnTableCodec()
{
  return turnTableCodec;
}
void MemsAutoTestSoftware::setTurnTableCodec(TurnTableCodec* turnTableCodec)
{
  if (Q_NULLPTR!=this->turnTableCodec)
    {
      disconnect(this->turnTableCodec, &TurnTableCodec::signal_turnTableError, this, &MemsAutoTestSoftware::slot_turnTableErrorEcho);
    }
  this->turnTableCodec = turnTableCodec;
  if(Q_NULLPTR!=this->turnTableCodec)
    {
      connect(this->turnTableCodec, &TurnTableCodec::signal_turnTableError, this, &MemsAutoTestSoftware::slot_turnTableErrorEcho);
    }
  printInjection(this,turnTableCodec);
}

ScriptSaveLoader* MemsAutoTestSoftware::getScriptSaveLoader()
{
  return scriptSaveLoader;
}
void MemsAutoTestSoftware::setScriptSaveLoader(ScriptSaveLoader* scriptSaveLoader)
{
  //  if (Q_NULLPTR != this->scriptSaveLoader)
  //    {
  //      disconnect(this->scriptSaveLoader, &ScriptSaveLoader::signal_saveScriptFileFailed, this, &MemsAutoTestSoftware::slot_saveScriptFileFailed);
  //      disconnect(this->scriptSaveLoader, &ScriptSaveLoader::signal_loadScriptFileFailed, this, &MemsAutoTestSoftware::slot_loadScriptFileFailed);
  //    }
  //  this->scriptSaveLoader = scriptSaveLoader;
  //  connect(this->scriptSaveLoader, &ScriptSaveLoader::signal_saveScriptFileFailed, this, &MemsAutoTestSoftware::slot_saveScriptFileFailed);
  //  connect(this->scriptSaveLoader, &ScriptSaveLoader::signal_loadScriptFileFailed, this, &MemsAutoTestSoftware::slot_loadScriptFileFailed);
}

TcpCodec_Transmit* MemsAutoTestSoftware::getTcpCodec_Transmit()const
{
  return tcpCodec_Transmit;
}

MemsAutoTestSoftware* MemsAutoTestSoftware::setTcpCodec_Transmit(TcpCodec_Transmit* const tcpCodec_Transmit)
{
  if(Q_NULLPTR!=this->tcpCodec_Transmit)
    {
      disconnect(this->tcpCodec_Transmit,&TcpCodec_Transmit::signal_result,this,&MemsAutoTestSoftware::slot_showResult);
    }
  this->tcpCodec_Transmit=tcpCodec_Transmit;
  connect(this->tcpCodec_Transmit,&TcpCodec_Transmit::signal_result,this,&MemsAutoTestSoftware::slot_showResult);
  printInjection(this,tcpCodec_Transmit);
  return this;
}

Dialog_SetThermostat* MemsAutoTestSoftware::getDialog_setThermostat()const
{
  return dialog_setThermostat;
}

MemsAutoTestSoftware* MemsAutoTestSoftware::setDialog_setThermostat(Dialog_SetThermostat* const dialog_setThermostat)
{
  this->dialog_setThermostat=dialog_setThermostat;
  printInjection(this,dialog_setThermostat);
  return this;
}

Dialog_SetLoadMode* MemsAutoTestSoftware::getDialog_SetLoadMode()const
{
  return dialog_SetLoadMode;
}


MemsAutoTestSoftware* MemsAutoTestSoftware::setDialog_SetLoadMode(Dialog_SetLoadMode* const dialog_SetLoadMode)
{
  this->dialog_SetLoadMode=dialog_SetLoadMode;
  printInjection(this,dialog_SetLoadMode);
  return this;
}

Dialog_SetRunningMode* MemsAutoTestSoftware::getDialog_SetRunningMode()const
{
  return dialog_SetRunningMode;
}

MemsAutoTestSoftware* MemsAutoTestSoftware::setDialog_SetRunningMode(Dialog_SetRunningMode* const dialog_SetRunningMode)
{
  this->dialog_SetRunningMode=dialog_SetRunningMode;
  printInjection(this,dialog_SetRunningMode);
  return this;
}

SetScriptDialog* MemsAutoTestSoftware::getSetScriptDialog()
{
  return setScriptDialog;
}
void MemsAutoTestSoftware::setSetScriptDialog(SetScriptDialog* setScriptDialog)
{
  this->setScriptDialog = setScriptDialog;
}

MainWindow* MemsAutoTestSoftware::getMainWindow()const
{
  return mainWindow;
}

MemsAutoTestSoftware* MemsAutoTestSoftware::setMainWindow(MainWindow*const mainWindow)
{
  this->mainWindow=mainWindow;
  printInjection(this,mainWindow);
  return this;
}

int MemsAutoTestSoftware::getAccuracy()
{
  return accuracy;
}

void MemsAutoTestSoftware::setAccuracy(int accuracy)
{
  this->accuracy = accuracy;
}

void MemsAutoTestSoftware::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setMainWindow(getObjFromMap<MainWindow>(objMap));
  setDialog_setThermostat(getObjFromMap<Dialog_SetThermostat>(objMap));
  setDialog_SetLoadMode(getObjFromMap<Dialog_SetLoadMode>(objMap));
  setDialog_SetRunningMode(getObjFromMap<Dialog_SetRunningMode>(objMap));
  //  setScriptSaveLoader(getObjFromMap<ScriptSaveLoader>(objMap));
  setDeviceInfoManager(getObjFromMap<DeviceInfoManager>(objMap));
  setTurnTableCodec(getObjFromMap<TurnTableCodec>(objMap));
  setTcpCodec_Transmit(getObjFromMap<TcpCodec_Transmit>(objMap));
}
